extends CharacterBody2D

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

# 最小倾斜度
@export var min_tilt_angle: int = -45
# 最大倾斜度
@export var max_tilt_angle: int = 45
# 向上飞的力度
@export var flap_power: int = 200
# 控制飞机上升速度
@export var max_velocity_y: int = 200


func _physics_process(delta):
	# Add the gravity.
	velocity.y += gravity * delta

	# 这只当前的倾斜角度
	var tilt_angle = clamp((velocity.y / gravity) * max_tilt_angle, min_tilt_angle, max_tilt_angle)
	self.rotation_degrees = tilt_angle

	move_and_slide()


func _unhandled_input(event):
	if event.is_action_pressed("flap"):
		self.velocity.y = clamp(self.velocity.y - flap_power, max_velocity_y * -1, max_velocity_y)
